<?php

if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Controller_Cover_Gril extends CI_Controller {
	private $m_current_period = null;
	private $m_last_period = null;
	
	public function __construct() {
		parent::__construct ();
		require(APPPATH.'helpers/protocal.php');
		$this->load->model ( 'Model_Cover_Gril', 'Cover_Gril' );
	}
	
	private function get_current_period() {
		require_once (BASEPATH . 'helpers/date_helper.php');
		$current_time = now ();
		$query = $this->Cover_Gril->get_current_period( );
		if ($query != null) {
			if ($current_time < strtotime($query->start_time) ) {
				log_message('error', 'current_time should not less than start_time');
			}
			else if ($current_time > strtotime($query->end_time) ) { //should change period
				$this->m_last_period = $query;
				$query = null;
			}
			else {
				$this->m_current_period = $query;
			}
		}
		if ($query == null) {
			$w = strftime ( '%w', $current_time );
			$start_time = null;
			if ($w >= START_COMPETITION_COVER_GRIL) {
				$w = $w - START_COMPETITION_COVER_GRIL;
			} else {
				$w = 7 - (START_COMPETITION_COVER_GRIL - $w);
			}
			$start_time = strtotime ( "-$w day", $current_time );
			$y = strftime ( '%Y', $start_time );
			$m = strftime ( '%m', $start_time );
			$d = strftime ( '%d', $start_time );
			$this->m_current_period->period = intval(sprintf ( '%s%s%s', $y, $m, $d)) ;
			$this->m_current_period->start_time = strftime ( "%Y-%m-%d %H:%M:%S", mktime ( 13, 0, 0, $m, $d, $y ) );
			$end_time = strtotime ( "+7 day", $start_time );
			$y = strftime ( '%Y', $end_time );
			$m = strftime ( '%m', $end_time );
			$d = strftime ( '%d', $end_time );
			$this->m_current_period->end_time = strftime ( "%Y-%m-%d %H:%M:%S", mktime ( 12, 59, 59, $m, $d, $y ) );
			$this->m_current_period->number = 0;
			$this->Cover_Gril->add_new_cover_gril_period( $this->m_current_period );
			$this->Cover_Gril->cover_gril_change_period ($this->m_current_period->period, $this->m_last_period->period);
		}
	}
	protected function cover_gril_reward_confirm() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$userid = $_GET ['userid'];
		$period = $_GET['period'];
		if (!$this->Cover_Gril->confirm_reward( $userid, $period ) ) {
			$result->return_code = 20001;
		}
		$this->output->set_output ( json_encode ( $result ) );
	}
	
	protected function cover_gril_weak() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$voted_userid = $_GET ['voted_userid'];
		$result->userid = $voted_userid;
		$ret = $this->Cover_Gril->cover_gril_weak ( $voted_userid );
		$this->output->set_output ( json_encode ( $result ) );
	}
	// param: type voter_userid voted_userid current_period
	// return_code: 1 vote failed;
	// return = { type; return_code; num_ticket; ranking; }
	protected function cover_gril_vote() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$voted_userid = $_GET ['voted_userid'];
		$period = $_GET ['current_period'];
		
		require_once (BASEPATH . 'helpers/date_helper.php');
		$current_time = strftime ( "%Y-%m-%d %H:%M:%S", now () );
		
		$ret = $this->Cover_Gril->cover_gril_vote ( $voted_userid, $period, $current_time );
		if (! $ret) {
			$result->return_code = 1;
		} else {
			$result->voted_userid = $voted_userid;
			$result->num_ticket = $ret->num_ticket;
			$result->ranking = $ret->ranking;
		}
		goto label_return;
		
		label_return:
		$this->output->set_output ( json_encode ( $result ) );
	}

	private function get_global_competition_info() {
		$result = null;
		
		$this->get_current_period ();
		
		$result->cg_current_period = $this->m_current_period;
		if ($this->m_last_period != null) {
			$result->cg_last_period = $this->m_last_period;
		}
		return $result;
	}
	
	public function get_user_competition_info() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET['protocal'] );
		
		if (!isset($_GET['version'])) {
			$result->return_code = 10001;
			goto label_return;
		}
		$version = floor( $_GET['version'] );
		if ($version < FASHION_VERSION) {
			$result->return_code = 10001;
			goto label_return;
		}
		
		$userid = $_GET['userid'];
		$this->get_current_period ();
		
		$result->cg_current_period = $this->m_current_period;
		if ($this->m_last_period != null) {
			$result->cg_last_period = $this->m_last_period;
		}
		$query = $this->Cover_Gril->get_current_period_by_userid ( $userid );
		if ($query->num_rows () > 0) {
			$result->current_period = $query->row ( 0 );
		}
		$query = $this->Cover_Gril->get_last_period_by_userid( $userid );
		if ($query->num_rows() > 0) {
			$result->last_period = $query->row( 0 );
		}
		$query1 = $this->Cover_Gril->get_cover_gril_data ( $userid );
		if ($query1 != null) {
			$result->global_cover_gril = $query1;
		}
        if ($query1 != null) {
			$result->reward = $query1;
		}

		$query2 = $this->Cover_Gril->get_cover_gril_total_person ( $this->m_current_period->period );
		if ($query2) {
			$result->current_period_total_count = $query2->number;
		}
		
		label_return:
		$this->output->set_output( json_encode( $result) );
	}
	// 上传 cover_gril
	// return_code: 1 have existedﾣﾻ 2 insert failed
	// return = { type; return_code; current_period = { }; cover_gril_count;
	// cover_gril_best_score; }
	protected function upload_cover_gril() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		
		$userid = $_POST ['userid'];
		$this->get_current_period ();
		if ($this->Cover_Gril->is_cover_gril_exist ( $userid, $this->m_current_period->period )) {
			goto label_have_exist;
		}
		$username = $_POST['username'];
		$this->Cover_Gril->add_user( $userid, $username);
		
		$plist = $_POST ['info'];
		$md5sum = $_POST ['md5sum'];
		$bg = $_POST ['bg'];
		require_once (BASEPATH . 'helpers/date_helper.php');
		$ret = $this->Cover_Gril->add_cover_gril ( $userid, $this->m_current_period->period, strftime ( "%Y-%m-%d %H:%M:%S", now () ), $plist, $md5sum, $bg );
		if (! $ret) {
			goto label_other_error;
		} else {
			$query = $this->Cover_Gril->get_cover_gril ( $userid, $this->m_current_period->period, ($this->m_last_period != null ? $this->m_last_period->period : null) );
			if ($query->num_rows () > 0) {
				$result->current_period = $query->row ( 0 );
			}
		}
		goto label_return;
		
		label_other_error:
		$result->return_code = 2;
		goto label_return;
		label_have_exist:
		$result->return_code = 1;
		goto label_return;
		
		label_return:
		$this->output->set_output ( json_encode ( $result ) );
	}
	
	// 返回cover gril 的列表
	// param period : int
	// param mode : 1 (latest upload) 2 (most voted) 0 (random) 4 (by name)
	// param page : int
	// param num_of_page : int
	// return = { type; return_code; [ cover_gril list ] }
	protected function get_current_period_cover_gril_list() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		$period = $_GET ['period'];
		$userid = $_GET['userid'];
		$mode = $_GET ['mode'];
		$start_page = $_GET ['start_page'];
		$num_per_page = $_GET ['num_per_page'];
		$num_page = $_GET ['num_page'];
		
		$ret = null;
		$start = $start_page * $num_per_page;
		$num = $num_page * $num_per_page;
		if ($mode == 0) {
			$ret = $this->Cover_Gril->get_cover_gril_list_by_random ( $period, $start, $num );
		} elseif ($mode == 1) {
			$ret = $this->Cover_Gril->get_cover_gril_list_by_latest ( $period, $start, $num );
		} elseif ($mode == 2) {
			require_once (BASEPATH . 'helpers/date_helper.php');
			$ret = $this->Cover_Gril->get_cover_gril_list_by_hottest ( $period, $start, $num, strftime ( "%Y-%m-%d %H:%M:%S", now () ) );
		}  elseif ($mode == 3) {
			$ret = $this->Cover_Gril->get_cover_gril_list_by_attention ( $userid, $period, $start, $num );
		} elseif ($mode == 4) {
			$ret = $this->Cover_Gril->get_cover_gril_list_by_fans ( $userid, $period, $start, $num );
		} elseif ($mode == 10) {
			$username = $_GET ['username'];
			$ret = $this->Cover_Gril->get_cover_gril_list_by_name ( $username, $period, $start, $num );
		}
		log_message ( 'debug', 'get_cover_list ' . $period );
		foreach ( $ret->result_array () as $row ) {
			$result->cover_gril_list [] = $row;
		}
		$this->output->set_output ( json_encode ( $result ) );
	}
	
	protected function get_last_period_cover_gril_list() {
		$result = null;
		$result->return_code = 0;
		$result->protocal = get_protocal( $_GET ['protocal'] );
		if (!isset($_GET['version'])) {
			$result->return_code = 10001;
			goto label_return;
		}
		$version = floor( $_GET['version'] );
		if ($version < FASHION_VERSION) {
			$result->return_code = 10001;
			goto label_return;
		}
		$start_page = $_GET ['start_page'];
		$num_per_page = $_GET ['num_per_page'];
		$num_page = $_GET ['num_page'];
		
		$ret = $this->get_global_competition_info ();
		$result->competition = $ret;
		
		$start = $start_page * $num_per_page;
		$num = $num_page * $num_per_page;
		$ret = $this->Cover_Gril->get_cover_gril_last_period ($start, $num );
		
		foreach ( $ret->result_array () as $row ) {	
			$result->cover_gril_list [] = $row;
		}
		
		label_return:
		$this->output->set_output ( json_encode ( $result ) );
	}
	
	public function index() {
		$protocal = get_protocal( $_GET['protocal'] );
		switch ($protocal) {
			case 1101 :
				$this->upload_cover_gril ();
				break;
			case 1102 :
				$this->get_user_competition_info ();
				break;
			case 1103 :
				$this->get_current_period_cover_gril_list ();
				break;
			case 1104 :
				$this->get_last_period_cover_gril_list ();
				break;
			case 1105:
				$this->cover_gril_vote ();
				break;
			case 1106 :
				$this->Cover_Gril->cover_gril_change_period ($this->m_current_period->period, $query->period);
				break;

			case 1107 :
				$this->cover_gril_reward_confirm();
				break;
			case 1108 :
				$this->cover_gril_weak();
				break;
			default :
				break;
		}
	}
}
